package com.love.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LayerOutPrint {

     static class TreeNode{

         public int val;

         public TreeNode left;

         public TreeNode right;

         public TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }

     }


     public static List<List<Integer>> pros( TreeNode root){
         List<List<Integer>> ans = new LinkedList<>();
         if(root == null){
             return ans;
         }
         Queue<TreeNode> queue = new LinkedList<>();
         queue.add(root);

         while (!queue.isEmpty()){
             int size = queue.size();
             ArrayList<Integer> nodes = new ArrayList<>();
             for (int i = 0; i < size; i++) {
                 TreeNode pollNode = queue.poll();
                 nodes.add(pollNode.val);

                 if(pollNode.left != null){
                     queue.add(pollNode.left);
                 }
                 if(pollNode.right != null){
                     queue.add(pollNode.right);
                 }
             }
             ans.add(0,nodes);
         }
         return ans;
     }

}
